/*

    tools23d - tools for converting images to 3D formats.
    Copyright (C) 2014  Dave Gorst

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/


/**
 * @file   cli-photo.c
 * @brief  Commandline option functions specific to photo23d.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>

#include "tools23d.h"
#include "photo23d.h"
#include "util.h"
#include "cli-common.h"
#include "cli-photo.h"
#include "util.h"
#include "list.h"

option app_options_table [26] =
{
    {T3D_AppUnknown,NULL,NULL},        /* 'a'  */
	{T3D_AppUnknown,NULL,NULL},        /* 'b'  */
    {T3D_SetColourScheme,"colour","Set colourscheme.\nValid values are colour and greyscale.\n\n"},        /* 'c'  */
    {T3D_AppUnknown,NULL,NULL},        /* 'd'  */
    {T3D_AppUnknown,NULL,NULL},        /* 'e'  */
    {T3D_AppUnknown,NULL,NULL},        /* 'f'  */
    {T3D_AppUnknown,NULL,NULL},        /* 'g'  */
    {T3D_AppUnknown,NULL,NULL},        /* 'h'  */
    {T3D_AppUnknown,NULL,NULL},        /* 'i'  */
    {T3D_AppUnknown,NULL,NULL},        /* 'j'  */
    {T3D_AppUnknown,NULL,NULL},        /* 'k'  */
    {T3D_SetLeft,"left","Give filename pattern for left image sequence.\n\n"},        /* 'l'  */
    {T3D_AppUnknown,NULL,NULL},        /* 'm'  */
    {T3D_AppUnknown,NULL,NULL},        /* 'n'  */
    {T3D_SetOutput,"output","Set output filename pattern."},        /* 'o'  */
    {T3D_AppUnknown,NULL,NULL},        /* 'p'  */
    {T3D_AppUnknown,NULL,NULL},        /* 'q'  */
    {T3D_SetRight,"right","Give filename pattern for right image sequence.\n\n"},        /* 'r'  */
    {T3D_AppUnknown,NULL,NULL},        /* 's'  */
    {T3D_AppUnknown,NULL,NULL},        /* 't'  */
    {T3D_AppUnknown,NULL,NULL},        /* 'u'  */
    {T3D_AppUnknown,NULL,NULL},        /* 'v'  */
    {T3D_AppUnknown,NULL,NULL},        /* 'w'  */
    {T3D_AppUnknown,NULL,NULL},        /* 'x'  */
    {T3D_AppUnknown,NULL,NULL},        /* 'y'  */
    {T3D_SetZoom,"zoom","Zoom in by a percentage factor.\n\n"}         /* 'z'  */
};

/**
@brief  Clear all photo23d-specific user options.

@param  cli_params  Pointer to user options structure
*/

void T3D_ClearAppParams (params * cli_params)
{
	cli_params->number_of_left_filenames = 0;
	cli_params->number_of_right_filenames = 0;
	cli_params->left_filenames = NULL;
	cli_params->right_filenames = NULL;
	cli_params->zoom = 100;
}

/**
@brief  Free all photo23d-specific user options.

@param  my_params   Pointer to user options structure
*/

void T3D_FreeAppParams (params * my_params)
{
	if (my_params->left_filenames != NULL) free (my_params->left_filenames);
	if (my_params->right_filenames != NULL) free (my_params->right_filenames);
}

/**
@brief  Clear all photo23d-specific user options.

@param  my_params   Pointer to user options structure
*/

void T3D_SetAppDefaults (params * my_params)
{
	/* None at present. */
}

/**
Function called when the option is not a known common or photo23d specific option.

@return	A ::t3d_return code indicating whether the option was set successfully - in this case will always be ::t3d_bad_parameter.
*/

OPTIONFUNCTION (T3D_AppUnknown)
{
	T3D_SetLastError ("Unknown command line option \"%c\".", input_char);
	return t3d_bad_parameter;
}

/**
@brief  Set the output filename pattern.

@return	A ::t3d_return code indicating whether the option was set successfully.
*/

OPTIONFUNCTION (T3D_SetOutput)
{
	cli_params->output_filename = argv [*arg];
	++*arg;
	return t3d_success;
}

/**
@brief  Set the output colour scheme.

@return	A ::t3d_return code indicating whether the option was set successfully.
*/

OPTIONFUNCTION (T3D_SetColourScheme)
{
	if (strcmp (argv [*arg], "colour") == 0)
		cli_params->colourscheme = Colour;
	else if (strcmp (argv [*arg], "greyscale") == 0)
		cli_params->colourscheme = Greyscale;
	else
	{
		T3D_SetLastError("Unknown colour scheme - should be colour or greyscale.");
		return t3d_bad_parameter;
	}

	++*arg;
	return t3d_success;
}


/**
@brief  Set the zoom level.

@return	A ::t3d_return code indicating whether the option was set successfully.
*/

OPTIONFUNCTION (T3D_SetZoom)
{
	cli_params->zoom = atoi (argv [*arg]);
	++*arg;
	if (!cli_params->zoom)
    {
        T3D_SetLastError("Zoom needs to be a percentage of the original size.");
        return t3d_bad_parameter;
    }
	return t3d_success;
}


/**
@brief  Set the left eye filename pattern.

@return	A ::t3d_return code indicating whether the option was set successfully.
*/

OPTIONFUNCTION (T3D_SetLeft)
{
	while ((*arg < argc) && (argv [*arg][0] != '-'))
		cli_params->left_filenames = T3D_AddToList ((unsigned int *) &(cli_params->number_of_left_filenames), cli_params->left_filenames, argv [(*arg)++]);

	return t3d_success;
}


/**
@brief  Set the right eye filename pattern.

@return	A ::t3d_return code indicating whether the option was set successfully.
*/

OPTIONFUNCTION (T3D_SetRight)
{
	while ((*arg < argc) && (argv [*arg][0] != '-'))
		cli_params->right_filenames = T3D_AddToList ((unsigned int *) &(cli_params->number_of_right_filenames), cli_params->right_filenames, argv [(*arg)++]);

	return t3d_success;
}

/**
@brief  Print the photo23d-specific banner text.

*/

void T3D_PrintBanner (void)
{
	T3D_OutputMessage
	(
		"\nphoto23d  Copyright (C) 2014 Dave Gorst\n"
		"=======================================\n\n"
		"This program comes with ABSOLUTELY NO WARRANTY.\n"
		"This is free software, and you are welcome to redistribute it\n"
		"under certain conditions.\n\n"
	);
}

/**
@brief  Print the photo23d-specific syntax text.

*/

void T3D_PrintHeader (void)
{
	T3D_OutputMessage
	(
		"Syntax : photo23d -l <left images> -r <right images> -o <output file pattern> [options]\n"
		"\nValid options are :\n\n"
	);
}

/**
Handle photo23d fixed args (currently none).

@param  cli_params  Pointer to user options structure.
@param  number_of_args  Number of arguments.
@param  arglist Pointer to list of arguments.
@return ::TRUE if sucessful, ::FALSE otherwise.
*/

BOOL T3D_HandleFixedArgs (params * cli_params, unsigned int number_of_args, char ** arglist)
{
	if (number_of_args)
	{
		T3D_SetLastError ("Photo23d does not currently take any fixed arguments.");
		return FALSE;
	}
	else
		return TRUE;
}
